package indi.gao.springAopTest.controller;

import indi.gao.springAopTest.service.OPService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * <p>Description:</p>
 * <p>Copyright: Copyright (c)2020$</p>
 * <p>Company: Tope</p>
 * <P>Created Date :2020$-05$-18$</P>
 * <P>@version 1.0</P>
 */
@RestController
public class ThreadTestController {

    @Resource
    private OPService opService;

    @RequestMapping(value = "/thread")
    public void threadTest() throws ExecutionException, InterruptedException {

        long begin = System.currentTimeMillis();

        CountDownLatch countDownLatch = new CountDownLatch(10);
        ArrayList<Future<Integer>> futures = new ArrayList<>();
        int result = 0;
        for (int i = 0; i < 10; i++) {
            Future<Integer> user = opService.findUser(countDownLatch);
            futures.add(user);
        }

        for (Future<Integer> future : futures) {
            result += future.get();
        }
        System.out.println("耗时：{}" + (System.currentTimeMillis() - begin));
    }
}
